﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Obsidian.Runtime;
using Obsidian.Edm;
using Oak.Core.Data;
using Oak.Base;
using Oak.Model;

namespace Oak.Core.Logic
{
    public class ActPermissionLG : OakLG<OakSession>
    {

        public ActPermissionLG() : base() { }

        public ActPermissionLG(OakSession session) : base(session) { }

        public CoActPermissionInfo AddPermission(CoActPermissionInfo perm, out string msg)
        {
            if (!VerifyPermission(perm, out msg))
                return null;

            perm.Created.Now();

            string str;
            CoActPermissionInfo dbPerm = this.GetPermission(
                perm.AppKeyId.Value, perm.ActionId.Value, out str);
            if (dbPerm != null)
            {
                msg = "";
                return dbPerm;
            }

            try
            {
                ActionLG actionLG = new ActionLG(Session);
                CoActionInfo act = actionLG.GetActionById(perm.ActionId.Value, out msg);
                if (act == null)
                    return null;
                perm.Save();
                perm.Action.Set(act);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return perm;

        }


        public CoActPermissionInfo UpdatePermission(CoActPermissionInfo perm, out string msg)
        {
            if (perm.ItemId.IsNull)
            {
                msg = "itemId不能为空";
                return null;
            }

            long itemId = perm.ItemId.Value;
            CoActPermissionInfo dbPerm = this.GetPermissionById(itemId, out msg);
            if (dbPerm == null)
                return null;

            dbPerm.ResetAssigned();

            if (perm.AppKeyId.IsNull)
                dbPerm.AppKeyId.Set(perm.AppKeyId.Value);

            if (perm.ActionId.IsNull)
                dbPerm.ActionId.Set(perm.ActionId.Value);

            try
            {
                dbPerm.Update();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return dbPerm;

        }

        public bool DeletePermissions(long[] itemIds, out string msg)
        {
            ActPermissionDA permDA = new ActPermissionDA();

            try
            {
                permDA.DeleteByItemIds(itemIds);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return false;
            }

            msg = "";
            return true;
        }


        public List<CoActPermissionInfo> GetPermissionList(CoActPermissionQry qry, out ListAttrInfo listAttr, out string msg)
        {

            if (!qry.CheckPagingAttrs(40, 200, out msg))
            {
                listAttr = null;
                return null;
            }

            ActPermissionDA permDA = new ActPermissionDA();
            List<CoActPermissionInfo> list;

            try
            {
                list = permDA.SelectList(qry, out listAttr);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                listAttr = null;
                return null;
            }

            msg = "";
            return list;

        }


        public CoActPermissionInfo GetPermissionById(long itemId, out string msg)
        {
            CoActPermissionInfo perm = OModel.GetByPk<CoActPermissionInfo>(itemId);
            if (perm == null)
            {
                msg = "该权限信息不存在";
                return null;
            }

            msg = "";
            return perm;
        }


        public bool HasPermission(string appKey, string actionName, out string msg)
        {
            CoActPermissionInfo perm = this.GetPermission(appKey, actionName, out msg);
            return perm != null;
        }


        public CoActPermissionInfo GetPermission(long appKeyId, long actId, out string msg)
        {
            AppKeyLG appKeyLG = new AppKeyLG(Session);
            CoActAppKeyInfo actAppKey = appKeyLG.GetAppKeyById(appKeyId, out msg);
            if (actAppKey == null)
                return null;

            ActionLG actionLG = new ActionLG(Session);
            CoActionInfo action = actionLG.GetActionById(actId, out msg);
            if (action == null)
                return null;

            ActPermissionDA permDA = new ActPermissionDA();
            CoActPermissionInfo perm;
            try
            {
                perm = permDA.SelectActPermission(
                     actAppKey.AppKeyId.Value, action.ActionId.Value);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return perm;

        }


        public CoActPermissionInfo GetPermission(string appKey, string actionName, out string msg)
        {
            AppKeyLG appKeyLG = new AppKeyLG(Session);
            CoActAppKeyInfo actAppKey = appKeyLG.GetAppKey(appKey, out msg);
            if (actAppKey == null)
                return null;

            ActionLG actionLG = new ActionLG(Session);
            CoActionInfo action = actionLG.GetActionByName(actionName, out msg);
            if (action == null)
                return null;

            ActPermissionDA permDA = new ActPermissionDA();
            CoActPermissionInfo perm;
            try
            {
                perm = permDA.SelectActPermission(
                     actAppKey.AppKeyId.Value, action.ActionId.Value);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return perm;

        }



        private bool VerifyPermission(CoActPermissionInfo perm, out string msg)
        {
            if (perm.AppKeyId.IsNull)
            {
                msg = "AppKeyId不能为空";
                return false;
            }

            if (perm.ActionId.IsNull)
            {
                msg = "ActionId不能为空";
                return false;
            }

            msg = "";
            return true;
        }

    }
}
